package edu.ncu.service.impl;

import edu.ncu.dao.FriendAskDao;
import edu.ncu.dao.UserDao;
import edu.ncu.entity.FriendAsk;
import edu.ncu.entity.User;
import edu.ncu.service.FriendAskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("friendAskService")
public class FriendAskServiceImpl implements FriendAskService {
    final FriendAskDao friendAskDao;
    final UserDao userDao;

    public FriendAskServiceImpl(FriendAskDao friendAskDao, UserDao userDao) {
        this.friendAskDao = friendAskDao;
        this.userDao = userDao;
    }

    /**
     * 根据接收者的id，找出所有发送者的用户信息，返回用户列表
     * @param receiverId
     * @return
     */
    @Override
    public List<User> findByUserId(int receiverId) {
        //找出所有收到的请求信息
        List<FriendAsk> friendAsks = friendAskDao.findByUserId(receiverId);
        System.out.println("发送的好友请求信息："+friendAsks);
        List<User> senders = new ArrayList<>();
        for(FriendAsk friendAsk : friendAsks)
        {
            //根据发送者的id，找出发送者所有信息
            User sender = userDao.findByUserId(friendAsk.getSenderId());
            senders.add(sender);
        }
        return senders;
    }

    /**
     * 更新请求信息
     * @param result
     * @param receiverId
     * @param senderId
     */
    @Override
    public void update(int result, int receiverId, int senderId) {
        friendAskDao.update(result, receiverId, senderId);
    }

    @Override
    public void saveFriendAsk(FriendAsk friendAsk) {
        friendAskDao.saveFriendAsk(friendAsk);
    }

    @Override
    public List<FriendAsk> findFriendAsk(int userId) {
        List<FriendAsk> asks = friendAskDao.findFriendAsk(userId) ;
        for (int i = 0; i < asks.size(); i++) {
            User sender = userDao.findByUserId(asks.get(i).getSenderId()) ;
            User receiver = userDao.findByUserId(asks.get(i).getReceiverId()) ;
            FriendAsk ask = asks.get(i) ;
            ask.setSender(sender);
            ask.setReceiver(receiver);
//            System.out.println("sender:"+sender);
//            System.out.println("receiver:"+receiver);
//            System.out.println(asks.get(i));
        }
        return asks ;
    }
}
